/* login-window.c
 *
 * Copyright 2024 wan-iot
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "login-config.h"
#include "login-window.h"

struct _LoginWindow
{
  GtkApplicationWindow  parent_instance;

  /* Template widgets */
  GtkHeaderBar        *header_bar;
  GtkButton *btn_login;
  GtkButton *btn_cancel;
  
  struct MainWindow *mainWindow;
  GtkApplicationWindow *main_window;
  GtkButton *btn_query;
};



G_DEFINE_TYPE (LoginWindow, login_window, GTK_TYPE_APPLICATION_WINDOW)

static void
login_window_class_init (LoginWindowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gtk_widget_class_set_template_from_resource (widget_class, "/a/b/c/login-window.ui");
  gtk_widget_class_bind_template_child (widget_class, LoginWindow, header_bar);
  gtk_widget_class_bind_template_child (widget_class, LoginWindow, btn_login);
  gtk_widget_class_bind_template_child (widget_class, LoginWindow, btn_cancel);
  //注册主窗体
  gtk_widget_class_bind_template_child (widget_class, LoginWindow, main_window);
  gtk_widget_class_bind_template_child (widget_class, LoginWindow, btn_query);
 
}
GtkWindow *window;
GtkWindow *main_window;
void ok(){
  g_print ("hello \n");
}

void login(){
  g_print("ok\n");
  //弹出消息框
  GtkWidget *dialog;
  dialog = gtk_message_dialog_new(window,
            GTK_DIALOG_DESTROY_WITH_PARENT,
            GTK_MESSAGE_INFO,
            GTK_BUTTONS_OK,
            "登录成功!");
  gtk_window_set_title(GTK_WINDOW(dialog), "登录结果提示");
  gtk_dialog_run(GTK_DIALOG(dialog));
  gtk_widget_destroy(dialog); 
}
void cancel(){
  GtkWidget *dialog;
  dialog = gtk_message_dialog_new(window,GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,"成功失败!");
  gtk_window_set_title(GTK_WINDOW (dialog),"登录结果提示");
  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);  
  g_print("打开新窗口----\n");
  //隐藏窗体
  gtk_widget_hide ((GtkWidget*)window);
  gtk_widget_show((GtkWidget*)main_window);

}
//关闭主窗体时，退出程序
void close_main_window(){
  g_print("关闭主窗体\n");
   // g_print("%s\n",;
    //gtk_widget_destroy ((GtkWidget*)window);
  //gtk_window_destroy(window);
  // TODO 拆分窗体
}
void main_btn(){
g_print("主窗体上的小按钮");
}
              
void btn_query(){
    g_print("query...\n");
}

static void
login_window_init (LoginWindow *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
  window = (GtkWindow*)self;
  main_window = (GtkWindow*)self->main_window;
          
  g_signal_connect (self->btn_login, "clicked", G_CALLBACK (login), NULL);
  g_signal_connect (self->btn_cancel,"clicked",G_CALLBACK (cancel),NULL);
              //第二个窗体
  g_signal_connect (main_window, "destroy", G_CALLBACK (close_main_window), NULL);
  g_signal_connect (self->btn_query, "clicked", G_CALLBACK (btn_query), NULL);
}
